window.loadInputdata = function (pid, outerDiv, viewDiv, callback, zoom) {
    var pro = $("<div/>").tempoprogressbar();
    outerDiv.empty();
    board = $('<div id="tempo-mine-input-flow-view-board"></div>')
        .appendTo(outerDiv).css({'height': 'inherit', width: outerDiv.innerWidth()});
    //注册洞察面板缩放事件，改变描述中流程图展示面板
    Eventbus.one('viewingResize.mine', function () {
        var coner = board.miningboard("getLeftAndTop");
        if (coner.left + 50 > outerDiv.innerWidth()) {
            board.css({width: coner.left + 50});
        } else {
            board.css({width: outerDiv.innerWidth()});
        }
        board.miningboard("updateSize");
    });
    $.ajax({
        url: contextPath + "/api/ai/mine/getObjectFlowInfoForView",
        data: {
            flowId: pid
        },
        type: 'post',
        dataType: 'json',
        success: function (params) {
            if (callback) callback(params)
            renderBoard(params || '{}', board);
            board.miningboard("updateSize");
            if (zoom) {
                var p = board.find('.tempo-board-container');
                var val = board.width() / board.find('.tempo-board-container').width();
                if (p[0].style.transform === undefined) {
                    p[0].style.zoom = val * 100 + "%";
                } else {
                    p[0].style.transform = "scale(" + val + ")";
                    p[0].style.transformOrigin = "top";
                }
                board.miningboard('setZoom', val, [0.5, 0]);
            }
        },
        complete: function () {
            pro.tempoprogressbar("close");
        },
        error: function () {

        }
    });
}

var comsflag = true;
/**
 * 渲染面板
 */
window.renderBoard = function (param, board, isCanBeEdit, name, needChange, ifNeedShowNodeInsight) {
    //如果流程失败，不让导出word,假如后续可以导出，注掉就可以
    if (param.failed) {
        $(".tempo-mine-board-headbar").find(".tempo-ui-icon-insight-msdoc").hide();
        $(".tempo-ui-icon-uparrows-undertint").parent().parent().width(132);
    }
    var isJsonObjct = function (param) {
        if (typeof (param) == "object" &&
            Object.prototype.toString.call(param).toLowerCase() == "[object object]" &&
            !param.length) {
            return true;
        } else {
            return false;
        }
    }
    var content = "{}";
    var failed = false;
    var failNodeIds = null;
    var successNodeIds = null;
    if (isJsonObjct(param)) {
        if (param.flowInfo) {
            content = param.flowInfo
            failed = param.failed;
            failNodeIds = param.failNodeId;
            successNodeIds = param.successNodeIds;
            content = JSON.parse(content);
        } else {
            content = param;
        }
    } else {
        content = JSON.parse(param);
    }
    if (!needChange) {
        content.children = processChildrenPosition(content.children).children;
    }
    // 修复流程修改名称后，洞察页面展示问题
    var analyseStatus = Eventbus.triggerHandler("getAnalyseStatus")[0];
    if (analyseStatus) {
        var id = analyseStatus.getMainId();
        var text = $('span[linkid=' + id + '] label').text();
        if (content.name != text && text) {
            content.name = text;
        }
    }
    if (name) {
        content.name = name;
    }
    if (window.currentInsightFlowName) {
        content.name = window.currentInsightFlowName;
    }
    //过滤出执行的挖掘流 End
    board.miningboard({
        canBeEdit: false,
        data: content,
        filter: '.tempo-groupmenu-item-text',
        dropOptions: {
            accept: '.tempo-groupmenu-item-text'
        },
        renderChild: function (data, parent) {
            if (failed) {
                var eid = data.eid;
                //下面的代码是处理洞察渲染类子流程内部节点状态的
                //判断执行成功的节点中是否包含当前节点  包含  则将改节点状态设置为“SUCCESS”
                if (successNodeIds != null) {
                    var successNodeIdArr = successNodeIds.split(",");
                    if (successNodeIdArr.indexOf(eid) != -1) {
                        data.status = "SUCCESS";
                    }
                }
                //判断执行失败的节点中是否包含当前节点  包含  则将改节点状态设置为“FAILED”
                if(failNodeIds!=null){
                    var failNodeIdArr=failNodeIds.split(",");
                    if(failNodeIdArr.indexOf(eid)!=-1){
                        data.status = "FAILED";
                    }
                }
            } else {
                //用于区分是版本管理洞察还是普通流程洞察
                if (window.isSnapshotInsight == undefined || (window.isSnapshotInsight != undefined && window.isSnapshotInsight != "true")) {
                    data.status = "SUCCESS";
                }
            }
            renderNode(data.style,
                data,
                parent,
                this.miningboard('getJspInstance'), isCanBeEdit);

        }
    });
    //初始化所有节点的位置
    board.miningboard("initSize");
    //更新执行节点的位置
    board.miningboard("updateSize");
    comsflag = content.comsflag;
    //是否需要展示节点洞察
    if(ifNeedShowNodeInsight && content.children && content.children.length > 0){
        var nodeInfo = content.children[0];
        var nodeId = nodeInfo.eid;
        var node = $("#" + nodeId);
        node.trigger('click', ['miningnode', nodeId, node]);
    }
    if (!comsflag) {
        var div = board.find(".tempo-minecommentnode.tempo-miningnode");
        div.hide();
    }

};

/**
 * 初始化面板的右键单击事件
 */
var viewinitBoardMenu = function (e, board, childNodes) {
    if (comsflag) {
        viewdataMenu = [
            {
                text: TempoUtils.locale("commentbox.hideallcomments"),
                //icon: 'ui-icon tempo-ui-icon tempo-ui-icon-paste',
                id: 'viewhidecoms',
                disabled: false
            }
        ];
    } else {
        viewdataMenu = [
            {
                text: TempoUtils.locale("commentbox.showallcomments"),
                //icon: 'ui-icon tempo-ui-icon tempo-ui-icon-paste',
                id: 'viewshowcoms',
                disabled: false
            }
        ];
    }
    var viewboardMenu = null;
    viewboardMenu = $("<ul/>").contextmenu({
        data: viewdataMenu,
        select: function (e, ui) {
            viewselectBoardMenu(e, ui, board, childNodes);
        },
        close: function () {
            viewboardMenu.contextmenu("enableAll").contextmenu("collapseAll");
        }
    }).appendTo($("body"));

    viewboardMenu.bind("contextmenu", function (e) {
        e.preventDefault();
    });
    $("body").bind('click.contextmenu', function (e) {
        viewboardMenu.contextmenu('hide');
    })

    //显示菜单
    viewboardMenu.contextmenu("show", {
        linkEl: board,
        position: {
            left: e.pageX,
            top: e.pageY
        }
    });
    e.preventDefault();
};

/**
 * 面板按钮选择事件
 */
var viewselectBoardMenu = function (e, ui, board, childNodes) {
    var id = ui.select.id;
    var div = board.find(".tempo-minecommentnode.tempo-miningnode");
    var ins = board.miningboard("getJspInstance");
    if ("viewshowcoms" == id) {
        div.show();
        /*for (var i = 0; i < childNodes.length; i++) {
            var childNodeData = childNodes[i];
            if(((childNodeData.nodeUuid != undefined)&&(childNodeData.nodeUuid != ""))&&((childNodeData.noteUuid != undefined)&&(childNodeData.noteUuid != ""))){
                //删除掉节点注释和节点之间的连线
                ins.connect({uuids: [childNodeData.nodeUuid, childNodeData.noteUuid]});
            }
        }*/
        comsflag = true;
    } else if ("viewhidecoms" == id) {
        /*for (var i = 0; i < childNodes.length; i++) {
            var childNodeData = childNodes[i];
            if(((childNodeData.nodeUuid != undefined)&&(childNodeData.nodeUuid != ""))&&((childNodeData.noteUuid != undefined)&&(childNodeData.noteUuid != ""))){
                //删除掉节点注释和节点之间的连线
                ins.detach({uuids: [childNodeData.nodeUuid, childNodeData.noteUuid]});
            }
        }*/
        div.hide();
        comsflag = false;
    }
};

/**
 * 处理节点位置
 */
window.processChildrenPosition = function (children) {
    var minX = Number.MAX_VALUE;
    var minY = Number.MAX_VALUE;
    var maxX = Number.MIN_VALUE;
    var maxY = Number.MIN_VALUE;
    var base = function (widget) {
        for (var name in $.tempo) {
            if (name == widget) {
                return
            }
        }
    }
    if (children) {
        //循环输出找出最小XY坐标
        for (var i = 0; i < children.length; i++) {
            var node = children[i];
            /*
             * 注释说明：目前流程图中的所有节点均为可视，所以不需要区分， 如有问题， 可使用下面代码判断
            var base = new $.tempo[node._widgetName]() || {options: {}};
            if(base.options.visiable) { }
            delete base;
            */
            if (!node.style) continue;

            minX = Math.min(minX, node.style.left);
            maxX = Math.max(maxX, node.style.left);
            minY = Math.min(minY, node.style.top);
            maxY = Math.max(maxY, node.style.top);
        }
        for (var i = 0; i < children.length; i++) {
            var node = children[i];
            /*
            var base = new $.tempo[node._widgetName]() || {options: {}};
            if(base.options.visiable) { }
            delete base;
            */
            if (!node.style) continue;

            node.style.left = minX > 20 ? node.style.left - minX + 20 : node.style.left;
            node.style.top = minY > 40 ? node.style.top - minY + 40 : node.style.top;
            if (node.style.top < 20) {
                node.style.top = 20;
            }
            if (node.style.left < 40) {
                node.style.left = 40;
            }
        }
    }

    return {
        children: children || [],
        minX: minX,
        maxX: maxX,
        minY: minY,
        maxY: maxY
    }
};
/**
 * 递归的根据连线的端点向上一级遍历节点
 * ++++++++++++++++++++++++++++++++++++++++方法暂时废弃
 */
window.filterContentChildren = function (childrenNode, childrenConn, endConn) {
    var children = [];
    for (var i = 0; i < childrenNode.length; i++) {
        var node = childrenNode[i];
        if (node.eid == endConn.from_op) {
            children.push(node);
        }
    }
    var conns = [];
    for (var int2 = 0; int2 < childrenConn.length; int2++) {
        var con = childrenConn[int2];
        for (var int3 = 0; int3 < children.length; int3++) {
            var node3 = children[int3];
            if (con.to_op == node3.eid) {
                conns.push(con);
                break;
            }
        }
    }
    for (var int4 = 0; int4 < conns.length; int4++) {
        children = children.concat(filterContentChildren(childrenNode, childrenConn, conns[int4]));
    }
    return children;
}
var nodeUuids = [];
window.renderNode = function (style, opts, container, jspInstance, isCanBeEdit) {
    if (opts.nodeType !== "commentbox") {
        var nodeUuid = opts.nodeUuid;
        if (!!nodeUuid) {
            nodeUuids.push(nodeUuid)
        }
        //节点注释
    } else if (opts.nodeType === "commentbox" && $.inArray(opts.nodeUuid, nodeUuids) === -1 && opts.parameters.comment_type !== "process_comment") {
        return
    }
    if (!style || !opts.nodeType) return;
    /*由于节点样式修改，需删除旧的高度和宽度*/
    if (opts.nodeType !== "commentbox") {
        delete style.width;
        delete style.height;
    }
    var div = $("<div/>").css(style);
    container.append(div);
    var config = window[opts.type][opts.nodeType];
    config = config || window[window.constant.MNODES][opts.nodeType];
    if (!config) {
        console.error("要加载的节点不存在，请检查");
        return;
    }
    var rendernode = config.renderNode;
    //将entry 输入接口参数传入到节点
    opts.entry = config.entry;
    // if(!window.onlyShowFlow) {
    // 	opts.status = "SUCCESS";
    // }
    rendernode = rendernode && rendernode != "" ? rendernode : "miningnode";
    var canBeEdit = false;
    if (isCanBeEdit) {
        var type = config.type;
        if (type == "dm_data") {
            canBeEdit = true;
        }
    }
    div[rendernode]($.extend(true, {}, {
        canBeEdit: canBeEdit,
        showHandle: false,
        data: opts,
        loadProp: true,
        draggable: false,
        containment: container,
        jspInstance: jspInstance,
        enableValidate: container.parent().miningboard('isAutoValidateNode'),
        board: container.parent()
    }));
};